<template>
  <view class="comBox">
    <sickTitleCom></sickTitleCom>
    <view class="body">
      <template v-for="(radioObj, index) in radioList">
        <view :key="index">
          <view class="name titleBold">{{ radioObj.name }}<span v-if="radioObj.noRequire">(非必填)</span></view>
          <view v-if="radioObj.tips" class="tips"> {{ radioObj.tips }} </view>
          <view class="sickList">
            <view
              v-for="item in radioObj.options"
              :key="item.id"
              :class="[radioObj.rowBlock === true ? 'sickBox blocks' : 'sickBox']"
            >
              <view
                :class="[checkValue[radioObj.key] === item.id ? 'sickSpan cur' : 'sickSpan']"
                @click="choseList(radioObj, item)"
              >
                {{ item.name }}
                <span v-if="checkValue[radioObj.key] === item.id" class="curImg"><img :src="curIcon" /></span>
              </view>
              <view v-if="item.tips" class="kidTips">{{ item.tips }}</view>
            </view>
          </view>
        </view>
      </template>
      <!-- 下一题上一题按钮 -->
      <nextProBtnCom @nextBtn="nextBtn" @proBtn="proBtn"></nextProBtnCom>
    </view>
  </view>
</template>

<script>
import nextProBtnCom from '@/components/nextProButton';
import sickTitleCom from './sickTitleCom';
import { ajaxSend } from './ajaxHealth.js';
import curIcon from '@/static/icon/cur.png';
export default {
  components: {
    // 上一题下一题组件
    nextProBtnCom,
    // 疾病大类title
    sickTitleCom
  },
  props: {
    radioList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      checkValue: {},
      curIcon: curIcon,
      noChose: false
    };
  },
  computed: {
    active() {
      return this.$store.getters.active;
    },
    assessForm() {
      return this.$store.getters.assessForm;
    },
    uSex() {
      return this.$store.getters.userSex;
    }
  },
  methods: {
    async nextBtn() {
      // 疾病直接提交接口
      let submitData = this.assessForm;
      let valueKeys = Object.keys(this.checkValue);
      if (valueKeys.length === 0) {
        uni.showToast({ title: `请先回答问题再提交`, icon: 'none' });
        return false;
      }
      for (let radio of this.radioList) {
        // console.log('radio', radio);
        if (!radio.noRequire && this.checkValue[radio.key] === undefined) {
          uni.showToast({ title: `请选择${radio.name}`, icon: 'none' });
          return false;
        }
      }
      let healthData = await ajaxSend(this.checkValue);

      submitData = Object.assign(submitData, this.checkValue);

      if (healthData && healthData.length > 0) {
        // 如果有异常，则提示
        this.$store.commit('assessQuestion/SET_AJAXRESMESSAGE', healthData); // 存提示信息到vux，数组格式
        this.$store.commit('assessQuestion/SET_AJAXRES', true); // 开启提示
      } else {
        this.$store.commit('assessQuestion/SET_ACTIVE', 3);
        this.$store.commit('assessQuestion/SET_AJAXRES', false);
      }
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 end
      this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
      wx.pageScrollTo({
        scrollTop: 0
      });
    },
    proBtn() {
      // let curActive = 0;
      // if (this.uSex && this.uSex === '男' && this.active === 8) {
      //   curActive = this.active - 2;
      // } else {
      //   curActive = this.active - 1;
      // }
      // curActive = this.active - 1;
      this.$store.commit('assessQuestion/SET_ACTIVE', 1);
    },
    choseList(radioObj, option) {
      // this.checkValue[radioObj.key] = option.id;
      this.$set(this.checkValue, radioObj.key, option.id);
    }
  }
};
</script>
<style lang="scss">
.comBox {
  background-color: #fff;
  .title {
    background: rgba(216, 246, 214, 1);
    color: rgba(0, 0, 0, 0.55);
    font-size: 32rpx;
    padding: 14rpx;
    text-align: center;
  }
  .body {
    padding: 24rpx;
    .name {
      padding: 20rpx 0;
    }
    .tips {
      color: rgba(0, 0, 0, 0.55);
      font-size: 28rpx;
      margin-bottom: 20rpx;
      padding: 0 16rpx;
    }
    .sickList {
      .sickBox {
        display: inline-block;
        width: 50%;
        .sickSpan {
          display: inline-block;
          text-align: center;
          width: 80%;
          padding: 10rpx 18rpx;
          margin: 12rpx 16rpx;
          border: 2rpx solid rgba(178, 178, 178, 1);
          border-radius: 16rpx;
          font-size: 28rpx;
          position: relative;
          .curImg {
            position: absolute;
            width: 40rpx;
            height: 40rpx;
            right: 0;
            bottom: 0;
            border: none;
            img {
              width: 40rpx;
              height: 40rpx;
            }
          }
        }
        .cur {
          border: 2rpx solid rgba(242, 85, 89, 1);
          color: rgba(242, 85, 89, 1);
        }
        .kidTips {
          color: rgba(0, 0, 0, 0.55);
          font-size: 28rpx;
          margin-bottom: 30rpx;
          padding: 0 16rpx;
        }
      }
      .blocks {
        display: block;
        width: 100%;
        .sickSpan {
          width: 590rpx;
        }
      }
    }
  }
}
.clear {
  clear: both;
  overflow: hidden;
  zoom: 1;
  height: 0px;
}
.lineHeight86 {
  height: 86rpx;
  line-height: 86rpx;
}
.size28 {
  font-size: 28rpx;
}
.titleBold {
  font-weight: 500;
}
</style>
